﻿using AutoMapper;
using AutoMapperTest.DTO;
using AutoMapperTest.Model;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutoMapperTest
{
    public class Startup
    {
        public Startup()
        {
            try
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                Mapper.Initialize(MappingConfiguration.Configure);

                sw.Stop();
                Console.WriteLine("Mapper初始化：" + sw.ElapsedMilliseconds);
                sw.Restart();
                Mapper.AssertConfigurationIsValid();
                sw.Stop();
                Console.WriteLine("Mapper规则验证：" + sw.ElapsedMilliseconds);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        public void fun1()
        {
            try
            {
                var dto = GetDefaultItemDTO();
                Stopwatch sw = new Stopwatch();
                sw.Start();
                InspItemRecord record = Mapper.Map<InspItemRecordDTO, InspItemRecord>(dto);
                record.IID = Guid.NewGuid().ToString();
                if (record.CoordinateDetails.ToList().Any())
                {
                    foreach (var s in record.CoordinateDetails)
                        s.CID = record.IID;
                }
                sw.Stop();
                Console.WriteLine("Item第一次转换耗时：" + sw.ElapsedMilliseconds);
                sw.Restart();
                var record2 = (new CustomerTypeConverter()).ManualConvertItem(dto);
                sw.Stop();
                Console.WriteLine("Item第二次转换耗时：" + sw.ElapsedMilliseconds);
                sw.Restart();
                InspItemRecordDTO rdto = Mapper.Map<InspItemRecord, InspItemRecordDTO>(record);
                sw.Stop();
                Console.WriteLine("Item第三次转换耗时：" + sw.ElapsedMilliseconds);
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("Item:{0}", ex.Message));
            }
        }
        public void fun2()
        {
            try
            {
                var dto = GetDefaultGroupDTO("Camera1");
                Stopwatch sw = new Stopwatch();
                sw.Start();
                InspGroupRecord record = Mapper.Map<InspGroupRecordDTO, InspGroupRecord>(dto);
                foreach (var item in record.Items)
                {
                    item.IID = record.ID;
                    item.ID = Guid.NewGuid().ToString();
                    foreach (var s in item.CoordinateDetails)
                        s.CID = item.ID;
                }
                sw.Stop();
                Console.WriteLine("Group第一次转换耗时：" + sw.ElapsedMilliseconds);
                sw.Restart();
                var record2 = (new CustomerTypeConverter()).ManualConvertGroup(dto);
                sw.Stop();
                Console.WriteLine("Group第二次转换耗时：" + sw.ElapsedMilliseconds);
                sw.Restart();
                InspGroupRecordDTO rdto = Mapper.Map<InspGroupRecord, InspGroupRecordDTO>(record);
                sw.Stop();
                Console.WriteLine("Group第三次转换耗时：" + sw.ElapsedMilliseconds);
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("Group:{0}", ex.Message));
            }

        }
        public void fun3()
        {
            try
            {
                var dto = GetDefaultRecordDTO("ABCDDDDDG");
                Stopwatch sw = new Stopwatch();
                sw.Start();
                //使用扩展方法，简化调用关系
                InspRecord record = dto.MapTo<InspRecord>();
                //Mapper.Map<InspRecordDTO, InspRecord>(dto);
                record.ConfigRecord();
                sw.Stop();
                Console.WriteLine("Record第一次转换耗时：" + sw.ElapsedMilliseconds);
                sw.Restart();
                var record2 = (new CustomerTypeConverter()).ManualConvertGroup(dto);
                sw.Stop();
                Console.WriteLine("Record第二次转换耗时：" + sw.ElapsedMilliseconds);
                sw.Restart();
                InspRecordDTO rdto = Mapper.Map<InspRecord, InspRecordDTO>(record);
                sw.Stop();
                Console.WriteLine("Record第三次转换耗时：" + sw.ElapsedMilliseconds);
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("Record:{0}", ex.Message));
            }

        }
        private InspItemRecordDTO GetDefaultItemDTO(int i = 1)
        {
            var dto = new InspItemRecordDTO
            {
                Brightness = 200,
                ExproseTime = 1000,
                InspedImg = new System.Drawing.Bitmap(1, 1),
                ItemIndex = -3,
                ItemMsg = "Test",
                ItemName = "测试项",
                ItemResult = false,
                ErrCol = new List<int> { i, i + 2, i + 3, i + 4, i + 5 },
                ErrDiff = new List<int> { i, i + 2, i + 3, i + 4, i + 5 },
                ErrRow = new List<int> { i, i + 2, i + 3, i + 4, i + 5 },
                ErrType = new List<int> { i, i + 2, i + 3, i + 4, i + 5 },
                ErrValue = new List<int> { i, i + 2, i + 3, i + 4, i + 5 }
            };
            return dto;
        }
        private InspGroupRecordDTO GetDefaultGroupDTO(string gname)
        {
            var dto = new InspGroupRecordDTO
            {
                GroupName = gname,
                Items = new List<InspItemRecordDTO> {
                    GetDefaultItemDTO(1),
                    GetDefaultItemDTO(3),
                    GetDefaultItemDTO(4),
                    GetDefaultItemDTO(5),
                    GetDefaultItemDTO(6),
                    GetDefaultItemDTO(7),
                    GetDefaultItemDTO(8),
                    GetDefaultItemDTO(11),
                }
            };
            dto.GroupResult = dto.Items.All(x => x.ItemResult);
            var types = new List<int>();
            dto.Items.ForEach(x => types.AddRange(x.ErrType.Distinct()));
            foreach (var s in types.Distinct())
                dto.ErrorType += s;
            return dto;
        }
        private InspRecordDTO GetDefaultRecordDTO(string pannelid)
        {
            var dto = new InspRecordDTO
            {
                PannelID    = pannelid,
                BatchID     = "批次1",
                Duaration   = 20,
                EndTime     = DateTime.Now,
                EquipID     = "API",
                LineNO      = "NO1",
                ModelID     = "0194",
                OperatorID  = "Operator1",
                StartTime   = DateTime.Now.AddSeconds(23),
            };
            dto.Groups = new List<InspGroupRecordDTO> { GetDefaultGroupDTO("Camera1"), GetDefaultGroupDTO("Camera2"), GetDefaultGroupDTO("Camera3") };
            dto.Groups.ForEach(x => dto.ErrCode = string.Format("{0},{1}", dto.ErrCode, x.ErrorType));
            return dto;
        }
    }
}
